package com.example.myapplication1.loldemo.behavior;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication1.R;
import com.example.myapplication1.loldemo.LolRecordQueryActivity;


/**
 * Copyright (C), 2020, 重庆八进制有限公司
 *
 * @author: weng
 * @last-modifier: weng
 * @version: 1.0
 * @create-date: 2020/11/23 16:18
 * @last-modify-date: 2020/11/23 16:18
 * @description:
 */
public class TabaleLayoutBehavior extends CoordinatorLayout.Behavior {

    private boolean isinit = false;
    //    背景高度，要滑动的距离
    private int heighttop;
    private int heightbootom;

    public TabaleLayoutBehavior() {
    }

    public TabaleLayoutBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {


        return dependency.getId() == R.id.headercontainer;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        if (!isinit) {
            isinit = true;
            int height = dependency.getMeasuredHeight();
            child.setTranslationY(height);
            Log.e("tag", "onDependentViewChanged");
        }

        return true;
    }


    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
        Log.e("tag", "onNestedPreScroll");

//        判断收拾是往上滑动还是往下滑动 dy>0 or dy <0
//        如果是往上滑动，判断child是否滑动到顶部，是否滑动到title下面；yes 将child固定
//        如果是往下滑动，先判断第一个条目是否可见，不可见，则滑动到第一个条目可见，然后再判断child是否滑动到初始位置，yes 就不允许往下滑动


        // 获取列表第一个全部可见Item的位置
        RecyclerView list = (RecyclerView) target;
        int pos = ((LinearLayoutManager) list.getLayoutManager()).findFirstCompletelyVisibleItemPosition();

        if (dy > 0) {
//           往上滑动
            if (child.getY() > heighttop) {
                consumed[1] = dy;
                child.setY(child.getY() - dy);
                target.setY(child.getY() + child.getHeight() - dy);
            } else {
                consumed[1] = 0;
                child.setY(heighttop);
                target.setY(heighttop + child.getHeight());
            }
        } else {

            if (pos == 0) {
                if (child.getY() < heightbootom) {
                    consumed[1] = dy;
                    child.setY(child.getY()-dy);
                    target.setY(child.getY() + child.getHeight() + dy);
                }else {
                    consumed[1] = 0;
                    child.setY(heightbootom);
                    target.setY(heightbootom + child.getHeight());
                }

            }
        }


//        Log.e("tag", "child.getY()-->" + child.getY());


    }


    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type, @NonNull int[] consumed) {
        Log.e("tag", "onNestedScroll");


    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        Log.e("tag", "onStartNestedScroll1");
        heighttop = coordinatorLayout.findViewById(R.id.rl_root).getHeight();
        heightbootom = coordinatorLayout.findViewById(R.id.headercontainer).getHeight();

        return true;
    }

    @Override
    public boolean onMeasureChild(@NonNull CoordinatorLayout parent, @NonNull View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
        return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
    }

    @Override
    public boolean onLayoutChild(@NonNull CoordinatorLayout parent, @NonNull View child, int layoutDirection) {
        return super.onLayoutChild(parent, child, layoutDirection);
    }
}
